package Ventanas.usuario;

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import util.SocketManager;

public class Ventana extends JFrame {

	private JPanel panel;
	public static int puerto = 3000;
	public static SocketManager manager;
	public static SocketManager managerLoc;

	public Ventana() {
		this.setVisible(true);
		setSize(800, 600);
		this.setPreferredSize(new Dimension(800, 600));
		setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);

		panel = new PanelInicial(this);

		pack();
	}

	public boolean conectar(String ip) {
		boolean conexion = false;
		JOptionPane.showMessageDialog(null, "Conectando a " + ip, ip,
				JOptionPane.INFORMATION_MESSAGE);

		try {
			manager = new SocketManager(ip, puerto);
			System.out.println("Conectado a la direccion" + ip);
			conexion = true;
			JOptionPane.showMessageDialog(null, "Conexion correcta", ip,
					JOptionPane.INFORMATION_MESSAGE);
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					"Error de conexion, intentelo mas tarde", ip,
					JOptionPane.ERROR_MESSAGE);
		}

		return conexion;

	}

	public static void escribir(String comando) {

		try {
			manager.Escribir(comando);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Has sido Expulsado",
					"USUARIO EXPULSADO ", JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}

	}

	public static String leer() {

		String respuesta = "";
		try {
			respuesta = manager.Leer();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Has sido Expulsado",
					"USUARIO EXPULSADO ", JOptionPane.ERROR_MESSAGE);
			System.exit(0);

		}
		return respuesta;
	}

	public static void salir() {

		try {
			manager.CerrarStreams();
			manager.CerrarSocket();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static SocketManager getSocketManager() {
		return manager;
	}

}
